package com.wonyesoft.wuliu.acticity;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SimpleAdapter;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.google.zxing.CaptureActivity;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.util.PreferencesCookieStore;
import com.wonyesoft.wuliu.AppContent;
import com.wonyesoft.wuliu.R;
import com.wonyesoft.wuliu.acticity.base.BaseActivity;
import com.wonyesoft.wuliu.api.GagApi;
import com.wonyesoft.wuliu.model.Address;
import com.wonyesoft.wuliu.model.CargoInfo;
import com.wonyesoft.wuliu.vo.JsonRequest;
import com.wonyesoft.wuliu.vo.JsonResponse;
import com.wonyesoft.wuliu.vo.State;

public class UserAddressActivity extends BaseActivity implements OnClickListener {

	private TabHost mTabHost;
	private RadioGroup mRadioGroup;
	private TextView title, head_right;
	private ImageView head_left;
	private ImageButton address_new;
	private ListView listview;
	private List<Map<String, Object>> list;
	private PreferencesCookieStore preferencesCookieStore = AppContent
			.getPreferencesCookieStore();
	private SharedPreferences share;
	private String phone, address, realname, mark;
	private HttpUtils http = new HttpUtils();
	public static Gson gson = new Gson();
	private RequestParams params;
	private List<Address> sourceDateList;

	
	@SuppressLint("NewApi")
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_address_listview);
		listview = (ListView) findViewById(R.id.address_list);
		title = (TextView) findViewById(R.id.head_title);
		address_new = (ImageButton) findViewById(R.id.address_new);
		head_left = (ImageView) findViewById(R.id.head_left);
		title.setText("地址管理");
		head_left.setOnClickListener(this);
		address_new.setOnClickListener(this);
		head_left.setVisibility(0);
	
	}
	
	private List<Map<String, Object>> getData() {
		list = new ArrayList<Map<String, Object>>();

		for (Address goods : sourceDateList) {

			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("address_name", goods.getLinkMan());
			// System.out.println("goods.getFlowId()"+goods.getFlowId());
			map.put("address_phone", goods.getMobile());
			// map.put("myorder_content_time", "2015 b0903");
			map.put("address_xiangxi", goods.getAddress());
			list.add(map);
		}

		return list;
	}

	private void getD() {
		Map<String, Object> dataMap = new HashMap<String, Object>();
		JsonRequest jr = JsonRequest.getInit();
		jr.setData(dataMap);
		String json = gson.toJson(jr);
		params = new RequestParams();
		params.addBodyParameter("jsonParam", json);
		http.configCookieStore(preferencesCookieStore);
		http.send(HttpRequest.HttpMethod.POST, GagApi.QUERYADDRESS, params,
				new RequestCallBack<String>() {

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {

						System.out.println("进入地址查询");
						Gson gson2 = new Gson();
						JsonResponse jrs = gson2.fromJson(responseInfo.result,
								JsonResponse.class);
						System.out.println(responseInfo.result);
						State state = jrs.getState();

						// System.out.println(preferencesCookieStore);
						if (state.getCode().equals(State.SUCEECCODE)) {
							// 存用户登录信息
							// System.out.println(responseInfo.result);

							if (gson2 != null) {
								Type type = new TypeToken<List<CargoInfo>>() {
								}.getType();
								try {
									sourceDateList = gson2.fromJson(
											new JSONObject(responseInfo.result)
													.getJSONObject("data")
													.getJSONArray("rows")
													+ "", type);
								} catch (JsonSyntaxException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								} catch (JSONException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
								if (null != sourceDateList)
									initViews();
							}

						} else
							System.out.println(state.getMsg());
						// ToastUtils.showLong(state.getMsg());

					}

					@Override
					public void onFailure(HttpException error, String msg) {
						// testTextView.setText(error.getExceptionCode() + ":" +
						// msg);
						System.out.println(msg);
					}
				});
	}

	protected void initViews() {
		SimpleAdapter adapter = new SimpleAdapter(this, getData(),
				R.layout.activity_address_content, new String[] {
						"address_name", "address_phone",
						"address_xiangxi" }, new int[] {
						R.id.address_name, R.id.address_phone,
						R.id.address_xiangxi });

		listview.setAdapter(adapter);

	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.head_left:
			finish();
			break;
		case R.id.address_new:
			openActivity(UserAddAddressActivity.class);
			break;
		default:
			break;
		}
	}


}
